package com.mfc.education.service.edu.controller.admin;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.mfc.education.common.base.result.R;
import com.mfc.education.common.base.result.ResultCodeEnum;
import com.mfc.education.common.base.util.ExceptionUtils;
import com.mfc.education.service.base.exception.EduException;
import com.mfc.education.service.edu.entity.excel.ExcelSubjectData;
import com.mfc.education.service.edu.entity.vo.SubjectVo;
import com.mfc.education.service.edu.listener.ExcelSubjectDataListener;
import com.mfc.education.service.edu.service.SubjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * <p>
 * 课程科目 前端控制器
 * </p>
 *
 * @author Mfc
 * @since 2021-07-25
 */
@Api(description = "课程分类管理")

@RestController
@RequestMapping("/admin/edu/subject")
@Slf4j
public class SubjectController {

    @Autowired
    private SubjectService subjectService;

    @ApiOperation("Excel批量导入课程分类")
    @PostMapping("import")
    public R uploadExcel(@ApiParam(value = "Excel文件",required = true) @RequestParam("file") MultipartFile file) {

        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
            subjectService.batchImport(inputStream);
        } catch (IOException e) {
            log.error(ExceptionUtils.getMessage(e));
            throw new EduException(ResultCodeEnum.FILE_UPLOAD_ERROR);
        }

        return R.ok().message("批量导入成功");

    }


    @ApiOperation("嵌数据列表")
    @GetMapping("nested-ist")
    public R nestedList(){


        List<SubjectVo> subjectVoList  = subjectService.nestedList();
        return R.ok().data("items", subjectVoList);
    }

}

